package com.shigu.test;

import com.opentae.data.mall.beans.MoBankLinkno;
import com.opentae.data.mall.beans.MoOrder;
import com.opentae.data.mall.beans.MoPassagewayAccount;
import com.opentae.data.mall.beans.MoSettlement;
import com.opentae.data.mall.examples.MoBankLinknoExample;
import com.opentae.data.mall.examples.MoSettlementExample;
import com.opentae.data.mall.interfaces.MoBankLinknoMapper;
import com.opentae.data.mall.interfaces.MoOrderMapper;
import com.opentae.data.mall.interfaces.MoPassagewayAccountMapper;
import com.opentae.data.mall.interfaces.MoSettlementMapper;
import com.shigu.main4.common.util.MoneyUtil;
import com.unpay.skb.requests.ReplacePayRequest;
import com.unpay.skb.requests.ReplacePaySelectRequest;
import com.unpay.skb.responses.ReplacePayResponse;
import com.unpay.skb.responses.ReplacePaySelectResponse;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.awt.geom.Arc2D;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class UnpaySettle extends BaseSpringTest{
    @Autowired
    MoSettlementMapper moSettlementMapper;

    @Autowired
    MoOrderMapper moOrderMapper;

    @Autowired
    MoPassagewayAccountMapper moPassagewayAccountMapper;

    @Autowired
    MoBankLinknoMapper moBankLinknoMapper;

    @Test
    public void testToday() throws ParseException {
        String day="20180208";
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
        MoSettlementExample example=new MoSettlementExample();
        Date today=sdf.parse(day);
        Calendar cal=Calendar.getInstance();
        cal.setTime(today);
        cal.add(Calendar.DATE,1);
        Date tom=cal.getTime();
        example.createCriteria().andSettlementTimeGreaterThan(today).andSettlementTimeLessThan(tom).andStatusEqualTo(1);
        List<MoSettlement> ms=moSettlementMapper.selectByExample(example);
        for(MoSettlement m:ms){
            String code=m.getSettlementCode();
            if (code != null) {
                ReplacePaySelectRequest req=new ReplacePaySelectRequest();
                req.setBatchNo("1");
                req.setPaidOrderId(code);
                req.setDateTime("20180209");
                req.setPaidType("0");
                req.setTranType("0");
                ReplacePaySelectResponse res=req.exec();
                System.out.println(m.getSettlementId()+"   "+res.resultBody());
            }
        }
    }

    @Test
    public void reSettlePay(){
        String paidOrderId="20180208235451003083";
        String newPaidOrderId="20180208235451103083";
        String payType="0";
        String money="508.00";
        MoSettlementExample example=new MoSettlementExample();
        example.createCriteria().andSettlementCodeEqualTo(paidOrderId);
        example.setStartIndex(0);
        example.setEndIndex(1);
        List<MoSettlement> setts=moSettlementMapper.selectByConditionList(example);
        if (setts.size()==0) {
            return;
        }
        MoSettlement sett=setts.get(0);
        MoOrder order=moOrderMapper.selectByPrimaryKey(sett.getOrderId());
        MoPassagewayAccount account=moPassagewayAccountMapper.selByUserId(order.getUserId(),order.getPassagewayId());
        MoBankLinknoExample linknoExample=new MoBankLinknoExample();
        linknoExample.createCriteria().andBankCardEqualTo(account.getBankNumber());
        MoBankLinkno linkno=moBankLinknoMapper.selectByExample(linknoExample).get(0);
        ReplacePayRequest req=new ReplacePayRequest();
        req.setBatchNo("1");
        req.setIdNo(account.getBodyNumber());
        req.setMerCbName(account.getBankName());
        req.setMerCbCode(linkno.getLinkno());
        req.setOrderId(order.getOrderCode());
        req.setPaidOrderId(newPaidOrderId);
        req.setPaidType(payType);
        req.setTranType("0");
        req.setPhoneNo(account.getTelephone());
        req.setSettAcct(account.getBankNumber());
        req.setSettAcctName(account.getRealName());
        req.setSubMchId(account.getAccountCode());
        req.setSubMchName(account.getRealName());
        Double settmoney=Double.valueOf(sett.getMoney());
        settmoney= Double.valueOf(StringUtils.isEmpty(money)? MoneyUtil.formatMoney(settmoney-2):money)*100;
        req.setTotalAmt(settmoney.longValue()+"");
        ReplacePayResponse res=req.exec();
        System.out.println(res.resultBody());

        MoSettlement upsett=new MoSettlement();
        upsett.setSettlementId(sett.getSettlementId());
        upsett.setSettlementCode(newPaidOrderId);
        moSettlementMapper.updateByPrimaryKeySelective(upsett);
    }
}
